﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace t_205_verk_hopur_12.Helpers
{
    // Controller helper class from NerdDinner tutorial
    public static class ControllerHelpers
    {
        public static void AddRuleViolations(this ModelStateDictionary modelState,
                                             IEnumerable<t_205_verk_hopur_12.Models.Channel.RuleViolation> error)
        {
            foreach (t_205_verk_hopur_12.Models.Channel.RuleViolation issue in error)
            {
                modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
            }
        }

        // This code is borrowed from StackOverflow user Sarcastic
        // His user profile is: http://stackoverflow.com/users/4048/sarcastic
        // The code appeared on the following page:
        // http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
        // It is used to find the start date of the week based on the day you want to be the first day
        // of the week, and is used by our ScheduleController to find the schedule for a given week.
        public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = dt.DayOfWeek - startOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }

            return dt.AddDays(-1 * diff).Date;
        }
        

        public static string RouteValues(this ViewContext context, string value)
        {
            if(context.Controller.ControllerContext.RouteData.Values[value] == null)
            {
                return null;
            }

            return context.Controller.ControllerContext.RouteData.Values[value].ToString();
        }
    }
}
